from django.db import models


class CronJob(models.Model):
    name = models.CharField(max_length=100,verbose_name="任务名称")
    command = models.CharField(max_length=100,unique=True,verbose_name="脚本名称")
    schedule = models.CharField(max_length=100,verbose_name="执行频率")  # 例如: '*/5 * * * *' (每5分钟)
    status_choice = (
        (1, "待启用"),
        (2, "已启用"),
    )
    status = models.IntegerField(choices=status_choice, default=1, verbose_name="状态")
    server_ids = models.JSONField(max_length=100,null=True, blank=True, verbose_name="目标主机")
    cron_script = models.TextField(null=True, blank=True, verbose_name="脚本内容")
    note = models.TextField(null=True, blank=True, verbose_name="备注")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    class Meta:
        db_table = "cronjob"
        verbose_name_plural = "任务管理"
        ordering = ('-id',)

    def __str__(self):
        return self.name

from django.db import models
from django_apscheduler.models import DjangoJob  # 假设这是正确的模型名称

class DjangoCronjob(models.Model):
    job = models.OneToOneField(DjangoJob, on_delete=models.CASCADE, related_name='extended')
    command = models.CharField(max_length=100,unique=True,verbose_name="脚本名称")
    schedule = models.CharField(max_length=100,verbose_name="执行频率")  # 例如: '*/5 * * * *' (每5分钟)
    status_choice = (
        (1, "待启用"),
        (2, "已启用"),
    )
    status = models.IntegerField(choices=status_choice, default=1, verbose_name="状态")
    server_ids = models.JSONField(max_length=100,null=True, blank=True, verbose_name="目标主机")
    cron_script = models.TextField(null=True, blank=True, verbose_name="脚本内容")
    note = models.TextField(null=True, blank=True, verbose_name="备注")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    class Meta:
        db_table = "django_cronjob"
        verbose_name_plural = "定时任务"
        ordering = ('-id',)

    def __str__(self):
        return self.job.id